package com.koron.order.prodForm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.CodeTools;
import com.koron.order.prodForm.bean.po.ExperimentForm;
import com.koron.order.prodForm.bean.query.ExperimentFormQuery;
import com.koron.order.prodForm.mapper.ExperimentFormMapper;
import com.koron.order.prodForm.service.ExperimentFormService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class ExperimentFormServiceImpl implements ExperimentFormService {

    @TaskAnnotation("getById")
    @Override
    public ExperimentForm getById(SessionFactory factory, String id) {
        ExperimentFormMapper mapper = factory.getMapper(ExperimentFormMapper.class);
        ExperimentForm experimentForm = mapper.selectById(id);
        return experimentForm;
    }

    @TaskAnnotation("list")
    @Override
    public List<ExperimentForm> list(SessionFactory factory, ExperimentFormQuery query) {
        ExperimentFormMapper mapper = factory.getMapper(ExperimentFormMapper.class);
        List<ExperimentForm> list = mapper.list(query);
        return list;
    }

    @TaskAnnotation("page")
    @Override
    public PageInfo<ExperimentForm> page(SessionFactory factory, PageQuery pageQuery, ExperimentFormQuery query) {
        return ExperimentFormService.super.page(factory, pageQuery, query);
    }

    @TaskAnnotation("save")
    @Override
    public boolean save(SessionFactory factory, ExperimentForm experimentForm) {
        Assert.notNull(experimentForm, "试验报表信息不能为空！");

        if (Objects.nonNull(experimentForm.getId())){
            return update(factory, experimentForm);
        }

        ExperimentFormMapper mapper = factory.getMapper(ExperimentFormMapper.class);
        boolean flag = mapper.save(experimentForm);

        List<Map<String, String>> equipments = experimentForm.getEquipments();
        if (CollectionUtil.isEmpty(equipments)) {
            return flag;
        }

        equipments.stream().forEach(map -> {
            String id = CodeTools.getCode32();
            String equipmentId = map.get("equipmentId");
            String equipmentName = map.get("equipmentName");
            String experimentFormId = experimentForm.getId();
            mapper.saveEquipment(id, experimentFormId, equipmentId, equipmentName);
        });

        return flag;
    }

    @TaskAnnotation("update")
    @Override
    public boolean update(SessionFactory factory, ExperimentForm experimentForm) {
        ExperimentFormMapper mapper = factory.getMapper(ExperimentFormMapper.class);
        boolean flag = mapper.update(experimentForm);

        List<Map<String, String>> equipments = experimentForm.getEquipments();
        if (CollectionUtil.isEmpty(equipments)) {
            return flag;
        }
        equipments.stream().forEach(map -> {
            String equipmentId = map.get("equipmentId");
            String equipmentName = map.get("equipmentName");
            String experimentFormId = experimentForm.getId();
            mapper.updateEquipment(experimentFormId, equipmentId, equipmentName);
        });

        return flag;
    }

    @TaskAnnotation("deleteById")
    @Override
    public boolean deleteById(SessionFactory factory, String id) {
        ExperimentFormMapper mapper = factory.getMapper(ExperimentFormMapper.class);
        return mapper.delete(id);
    }
}
